<meta charset="utf-8" />
<?php
//session_start();
//if(!isset($_SESSION["adminname"]))
//    header("location:/final_project/login/login_ht.php?msg=您没有权限，请登录后访问！");
//?>
<?php
//if (isset($_GET["msg"])) {
//    echo $_GET["msg"];
//}
//?>

<?php
//使用$_POST数组获取表单中输入的修改后的数据
$uid = $_POST['uid'];
$uname = trim($_POST['uname']);
$uname = trim($_POST['password']);
$birthdate = $_POST['birthdate'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$filename=$_FILES["file"]["name"];
$pic = $_POST['pic'];
//连数据库
require ('../Common/dbConn.php');

//上传文件错误的判定
if($_FILES["pic"]["error"]>0)
{
    switch($_FILES["pic"]["error"])
    {
        case 1: echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值"; exit;
        case 3: echo "部分文件上传";  exit;
        case 4: echo "没有选择头像文件!";
            //如果没选择图片，则编写更新其他数据的sql语句
            $sql = "update users set uname='{$uname}', birthdate='{$birthdate}', gender='{$gender}',email='{$email}' where uid={$uid}";
            break;
        default: echo "未知错误"; exit;
    }
}else {   //上传文件正常，更新数据表

//2.上传文件判断类型
    $allowtype=array("jpg","jpeg","png","gif","bmp","flv");
    $arr=explode(".",$_FILES["pic"]["name"]);
    $suffix=$arr[count($arr)-1];
    if(! in_array($suffix,$allowtype)){
        echo "文件类型错误！";
        exit;
    }else{
        echo"<br>照片已上传<br>";
    }
//3.拷贝文件
    $filepath="../../images/user/";
    $randname=date("YmdHis").".".$suffix;
    move_uploaded_file($_FILES["pic"]["tmp_name"],$filepath.$randname);
    //获取原头像文件的文件名
    $sql="select * from users where uid={$uid}";
    $result=mysql_query($sql);
    $row=mysql_fetch_assoc($result);
    $filename=$filepath.$row["pic"];
    //删除原来的头像文件
    if(file_exists($filename))
        unlink($filename);
    $sql = "update users set uname='{$uname}',password='{$password}', birthdate='{$birthdate}', gender={$gender}, email='{$email}', pic='{$randname}'  where uid={$uid}";
}

//执行SQL语句
$result = mysql_query($sql) or die("sql={$sql}, 更新失败！<br/>".mysql_error());
//判断是否更新成功
$num = mysql_affected_rows();//返回前一次 MySQL 操作所影响的记录行数
if($num!=1){
    echo "更新失败！<br/>";
    header("location:'userList.php'");
}else{
    echo "更新成功，3秒返回用户列表页";
    header("location:'userList.php'");
}
?>
